/**
 * 
 */
package com.jimet.utils.mail;


import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.jimet.utils.mail.SMTPAuthenticator;




/**
 * @author aliy
 *
 */

public class Mailer {

	private String TO;
	private String FROM;
	private String receiverName = "";
	private String subject="";
	private String body="";
	private String fromName = "";
	private String recipientsBCC = "";
	private String[] filePath = null;
	
	public String[] getFilePath() {
		return filePath;
	}

	public void setFilePath(String[] filePath) {
		this.filePath = filePath;
	}

	public Mailer(){
		
	}

	public void addToBody(String str)
	{
		this.body += str + "\n";
	}
	
	public void addImg(String str,String width, String height)
	{
		this.body += "<img src=\""+str+"\" border=0 width="+width+" height="+height+">"+ "\n";
	}
	
	public void sendMail(String smtpHost, String smtpPort , String username, String password)
	{

        try
        {

            Properties props = new Properties();
            props.put("mail.smtp.host",smtpHost);
            props.put("mail.smtp.port", smtpPort );
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "false");
            props.put("mail.smtp.socketFactory.port", smtpPort);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");            

            Authenticator auth = new SMTPAuthenticator();
            
            Session s = Session.getInstance(props,auth);
			s.setDebug(false); 
			MimeMessage message = new MimeMessage(s);

            InternetAddress From = new InternetAddress(this.FROM,this.fromName,"UTF-8");
            message.setFrom(From);
            InternetAddress To = new InternetAddress(this.TO,this.receiverName,"UTF-8");
            message.setRecipient(Message.RecipientType.TO, To);            

    		String[] recepientsList = this.getRecipientsBCC().split(";");
    		
    		int sizeTo					= recepientsList.length;
    		int itemIx					= 0;
    		InternetAddress[] addressCC = new InternetAddress[sizeTo];
    		for (int i = 0; i < sizeTo; i++)
    		{
    			if ( !recepientsList[i].equals("") )
    				addressCC[itemIx++] = new InternetAddress(recepientsList[i].toString()) ;
    		}
    		if ( itemIx > 0 )
    			message.setRecipients(Message.RecipientType.BCC, addressCC);
    		
 
    		// create and fill the first message part
    		
    		Multipart mp	 	= new MimeMultipart();
    	    MimeBodyPart mbp1 	= new MimeBodyPart();
    	    
    	    String body = "<table width=\"600\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">" +
    	    		"<tr><td colspan=\"5\" style=\"border-color:navy;border-bottom:2px solid;\"><img src=\"http://91yazilim.com.tr/site/mail_header.png\"></td></tr>" +
    	    		"<tr><td colspan=\"5\" style=\"height:2px;background:navy;\"></td></tr>" +
    	    		"<tr><td  style=\"background-color:#f2f2f2;width:35px;\"></td>" +
    	    		"<td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"width:450px;height:40px;\"></td>" +
    	    		"<td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"background-color:#f2f2f2;width:35px;\"></td></tr>" +
    	    		"<tr><td  style=\"background-color:#f2f2f2;width:35px;\"></td>" +
    	    		"<td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"width:450px;height:40px;\">";
    	    body += this.body;
    	    body += "</td><td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"background-color:#f2f2f2;width:35px;\"></td></tr>" +
    	    		"<tr><td  style=\"background-color:#f2f2f2;width:35px;\"></td>" +
    	    		"<td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"width:450px;height:40px;\"></td>" +
    	    		"<td  style=\"width:40px;\"></td>" +
    	    		"<td  style=\"background-color:#f2f2f2;width:35px;\"></td></tr>" +
    	    		"<tr><td colspan=\"5\" style=\"background-color:#f2f2f2;height:40px;\"></td></tr></table>";
    	    String bodyPart		= this.embedBodyToTemplate(body);
    	    mbp1.setText(bodyPart,"UTF-8");
    	    mbp1.setContent(bodyPart, "text/html; charset=UTF-8");
    	    mp.addBodyPart(mbp1);
    	    
      		if(this.getFilePath()!=null)
    		{
      			for(int i=0;i<filePath.length;i++)
        		{
      				// attach the file to the message
      				mbp1	= new MimeBodyPart();
      	      		FileDataSource fds = new FileDataSource(this.getFilePath()[i]);
      	      		mbp1.addHeader("CONTENT-TYPE","application/octet-stream");
      	      		mbp1.setDataHandler(new DataHandler(fds));
      	      		mbp1.setFileName(fds.getName());
      	    		mbp1.setDisposition(MimeBodyPart.ATTACHMENT);
      	      		mp.addBodyPart(mbp1);
        		}  			
    		}
    		
			message.setSubject(this.subject,"UTF-8");
			message.setContent(mp);
			//message.setContent(bodyPart, "text/html; charset=UTF-8");
	        message.setSentDate(new java.util.Date());

			
			Transport transport = s.getTransport("smtp");
			transport.connect(smtpHost, username, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();	
			
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
	}
	
	private String embedBodyToTemplate ( String bodyTxt )
	{
		String resultStr		= "";
		
		resultStr				+= bodyTxt;
		resultStr				+= "<p><font style=\"font-size:8pt;color:#999999\">Reachol-Aktivasyon - 2014</font></p>";
		
		return resultStr;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return Returns the body.
	 */
	public String getBody() {
		return body;
	}

	/**
	 * @param body The body to set.
	 */
	public void setBody(String body) {
		this.body = body;
	}

	/**
	 * @return Returns the subject.
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject The subject to set.
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return Returns the tO.
	 */
	public String getTO() {
		return TO;
	}

	/**
	 * @param to The tO to set.
	 */
	public void setTO(String to) {
		TO = to;
	}

	
	/**
	 * @return Returns the FROM.
	 */
	public String getFROM() {
		return FROM;
	}

	/**
	 * @param from The FROM to set.
	 */
	public void setFROM(String from) {
		FROM = from;
	}
	
	/**
	 * @return Returns the receiverName.
	 */
	public String getReceiverName() {
		return receiverName;
	}

	/**
	 * @param receiverName The receiverName to set.
	 */
	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	/**
	 * @return Returns the fromName.
	 */
	public String getFromName() {
		return fromName;
	}

	/**
	 * @param fromName The fromName to set.
	 */
	public void setFromName(String fromName) {
		this.fromName = fromName;
	}



	/**
	 * @return Returns the recipientsBCC.
	 */
	public String getRecipientsBCC() {
		return recipientsBCC;
	}



	/**
	 * @param recipientsBCC The recipientsBCC to set.
	 */
	public void setRecipientsBCC(String recipientsCC) {
		this.recipientsBCC = recipientsCC;
	}

}





